package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DzZhishiMapper;
import com.ruoyi.system.domain.DzZhishi;
import com.ruoyi.system.service.IDzZhishiService;


import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.mapper.DzHuiyuanMapper;
import com.ruoyi.system.domain.DzHuiyuan;
import com.ruoyi.system.service.IDzHuiyuanService;

/**
 * 知识点Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-02
 */
@Service
public class DzZhishiServiceImpl implements IDzZhishiService 
{
    @Autowired
    private DzZhishiMapper dzZhishiMapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IDzHuiyuanService dzHuiyuanService;

    /**
     * 查询知识点
     * 
     * @param zsId 知识点主键
     * @return 知识点
     */
    @Override
    public DzZhishi selectDzZhishiByZsId(Long zsId)
    {
        return dzZhishiMapper.selectDzZhishiByZsId(zsId);
    }

    /**
     * 查询知识点列表
     * 
     * @param dzZhishi 知识点
     * @return 知识点
     */
    @Override
    public List<DzZhishi> selectDzZhishiList(DzZhishi dzZhishi)
    {

        //权限控制
        SysUser user=  SecurityUtils.getLoginUser().getUser();
        List<SysRole> listRole= sysRoleService.selectRolesByUserId(user.getUserId());
        int datascope = 5;
        for(SysRole role:listRole){
            if( role.getDataScope()!=null&&Integer.parseInt(role.getDataScope())<datascope){
                datascope=Integer.parseInt(role.getDataScope());
            }
        }
        dzZhishi.setDatascope(datascope);
        if(datascope==4){
            dzZhishi.setCheckDeptId(user.getDeptId());
        }
        if(datascope==5){
            DzHuiyuan huiyuan =  dzHuiyuanService.selectDzHuiyuanByDn(user.getPhonenumber());
            if(huiyuan!=null){
                dzZhishi.setCheckHuiyuanId(huiyuan.getId());
            }else{
                dzZhishi.setCheckHuiyuanId(0l);
            }

        }
        return dzZhishiMapper.selectDzZhishiList(dzZhishi);
    }

    @Override
    public List<DzZhishi> selectDzZhishiListWeb(DzZhishi dzZhishi)
    {
        return dzZhishiMapper.selectDzZhishiList(dzZhishi);
    }

    /**
     * 新增知识点
     * 
     * @param dzZhishi 知识点
     * @return 结果
     */
    @Override
    public int insertDzZhishi(DzZhishi dzZhishi)
    {
        dzZhishi.setCreateTime(DateUtils.getNowDate());
        return dzZhishiMapper.insertDzZhishi(dzZhishi);
    }

    /**
     * 修改知识点
     * 
     * @param dzZhishi 知识点
     * @return 结果
     */
    @Override
    public int updateDzZhishi(DzZhishi dzZhishi)
    {
        return dzZhishiMapper.updateDzZhishi(dzZhishi);
    }

    /**
     * 批量删除知识点
     * 
     * @param zsIds 需要删除的知识点主键
     * @return 结果
     */
    @Override
    public int deleteDzZhishiByZsIds(Long[] zsIds)
    {
        return dzZhishiMapper.deleteDzZhishiByZsIds(zsIds);
    }

    /**
     * 删除知识点信息
     * 
     * @param zsId 知识点主键
     * @return 结果
     */
    @Override
    public int deleteDzZhishiByZsId(Long zsId)
    {
        return dzZhishiMapper.deleteDzZhishiByZsId(zsId);
    }
}
